package com.github.shiqiyue.learn.guava;

import com.google.common.math.IntMath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.RoundingMode;

/***
 * 描述
 * @author wenyao.wu
 * @date 2018/7/3
 */
public class IntMathTest {

    private static final Logger logger = LoggerFactory.getLogger(IntMathTest.class);

    public static void main(String[] args) {
        try {
            // 检查相加结果是否超过范围
            logger.info("{}", IntMath.checkedAdd(Integer.MAX_VALUE, Integer.MAX_VALUE));
        } catch (ArithmeticException e) {
            logger.info("Error: " + e.getMessage());
        }

        logger.info("{}", IntMath.divide(100, 5, RoundingMode.UNNECESSARY));
        try {
            // RoundingMode.UNNECESSARY, 如果除不尽则报错误
            logger.info("{}", IntMath.divide(100, 3, RoundingMode.UNNECESSARY));
        } catch (ArithmeticException e) {
            logger.info("Error: " + e.getMessage());
        }
        //四舍五入
        logger.info("{}", IntMath.divide(10, 4, RoundingMode.HALF_UP));
        // log2
        logger.info("Log2(2): {}", IntMath.log2(2, RoundingMode.HALF_EVEN));
        // log10
        logger.info("Log10(10): {}", IntMath.log10(10, RoundingMode.HALF_EVEN));
        // sqrt
        logger.info("sqrt(100): {}", IntMath.sqrt(IntMath.pow(10, 2), RoundingMode.HALF_EVEN));
        // 最大公约数
        logger.info("gcd(100,50): {}", IntMath.gcd(100, 50));
        // mod
        logger.info("modulus(100,50): {}", IntMath.mod(100, 50));
        // 阶乘
        logger.info("factorial(5): {}", IntMath.factorial(5));
    }

}
